<?php

namespace App\Logics;

use App\Models\Admin;
use App\Models\SiteAdv;
use App\Models\SiteCate;
use App\Models\SiteConfig;
use App\Models\SiteDomain;
use App\Models\SiteInfo;
use App\Models\SiteRss;
use App\Models\SiteTag;
use App\Service\Base\Logic;
use App\Service\Translate;
use App\Service\Tree;
use Illuminate\Support\Facades\Config;

class SiteLogic extends Logic
{

    // 首页广告
    public function adv()
    {
        $space = $this->option()->getData('space');
        $where = ['terminal' => 'app', 'status' => 1];
        $where = ['status' => 1];
        if($space){
            $where[] = ['space', '=', $space];
        }
        $list = (new SiteAdv())->isCache(true)->getList($where);

        return $list;
    }

    // 邮件订阅
    public function emailSubscripte()
    {
        $option = $this->option();
        $email  = $option->getData('email', null, 'trim');
        if(empty($email)){
            return $this->_returnFail('请指定订阅邮件！');
        }

        $model = new SiteRss();
        $res   = $model->onRss($email);
        if($res == false){
            return $this->_returnFail($model->getError());
        }

        return $res;
    }


    // 取消邮件订阅
    public function emailCancel()
    {
        $option = $this->option();
        $email  = $option->getData('email', null, 'trim');
        if(empty($email)){
            return $this->_returnFail('请指定订阅邮件！');
        }

        $model = new SiteRss();
        $res   = $model->cancel($email);
        if($res == false){
            return $this->_returnFail($model->getError());
        }

        return $res;
    }


    // 站点配置信息
    public function config()
    {
        $lists  = (new SiteConfig())->isCache(true)->getConfig();
        $config = array_column($lists, 'value', 'field');

        return $config;
    }

    // 获取所有的栏目
    public function cates($order = 'rank ASC', $where = [])
    {
        $where = $where ?: [['status', '=', 1]];
        $lists = (new SiteCate())->isCache(true)->getAll($where, $order);
        if(!empty($lists)){
            $tree  = new Tree();
            $lists = $tree->toTree($lists); // 树状
        }

        return $lists;
    }

    // 获取所有的栏目
    public function cateInfo($id)
    {
        $where = ['id' => $id];
        $info  = (new SiteCate())->isCache(true)->oneFind($where);

        return $info;
    }

    // 获取所有的标签
    public function tags($number = 0, $orderBy = 'id DESC')
    {
        $where = ['status' => 1];
        if($number){
            $lists = (new SiteTag())->isCache(true)->getList($where, $number, $orderBy);
        } else{
            $lists = (new SiteTag())->isCache(true)->getAll($where, $orderBy);
        }

        return $lists;
    }

    // 获取所有的标签
    public function getTag($tag)
    {
        $where = ['status' => 1, 'en_title' => $tag];
        $info  = (new SiteTag())->isCache(true)->oneFind($where);

        return $info;
    }

    // 翻译
    public function translate()
    {
        $opt  = $this->option();
        $text = $opt->getData('text', null, 'trim');
        $lang = $opt->getData('language', 'en', 'trim');
        if(empty($text)){
            return $this->_returnFail('请输入要翻译的文字！');
        }

        // 执行
        $result = (new Translate())->translateTo($text, $lang);

        return $result;
    }


    // 站点：帮助中心
    // 站点：联系我们
    // 站点：隐私协议
    // 站点：用户协议
    public function info($id)
    {
        $info = (new SiteInfo())->isCache(true)->oneFind(['id' => $id]);

        return $info;
    }


}


